/*Diego Lima de Freitas RA: 317632
 *Yuri Souza Padua RA: 317420
 * 
 * Classe que trata os erros do programa
 */
package compiler;

import Lexer.Lexer;
import java.io.PrintWriter;

public class CompilerError {

    private Lexer lexer;
    private PrintWriter out;
    private boolean thereWasAnError;

    public CompilerError(Lexer lexer, PrintWriter out) {
        this.lexer = lexer;
        this.out = out;
        thereWasAnError = false;
    }

    public void setLexer(Lexer lexer) {
        this.lexer = lexer;
    }

    public boolean wasAnErrorSignalled() {
        return thereWasAnError;
    }

    public void show(String strMessage) {
        show(strMessage, false);
    }

    public void show(String strMessage, boolean goPreviousToken) {
        if (goPreviousToken) {
            out.println("Erro na linha " + lexer.getLineNumberBeforeLastToken() + ": ");
            out.println(lexer.getLineBeforeLastToken());
        } else {
            out.println("Erro na linha " + lexer.getLineNumber() + ": ");
            out.println(lexer.getCurrentLine());
        }

        out.println(strMessage);
        out.flush();
        if (out.checkError()) {
            System.out.println("Erro em sinalizar um erro");
        }
        thereWasAnError = true;
    }

    public void signal(String strMessage) {
        show(strMessage);
        out.flush();
        thereWasAnError = true;
        throw new RuntimeException();
    }
}
